package cn.net.ecode.util;

import cn.net.ecode.common.config.Global;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * @ClassName SendSmsUtils
 * @Description 发送短信验证码工具类
 * @Author AlanShelby
 * @Date 2019-08-26 15:21:22 15:21
 * @Version 1.0
 */
public class SendSmsUtils {

    private static final String ACCESSKEY_ID = Global.getProperty("aliyun.sms.accessKeyId");
    private static final String SECRET = Global.getProperty("aliyun.sms.secret");
    private static final String TEMPLATE_CODE = Global.getProperty("aliyun.sms.TemplateCode");
    private static final String SIGN_NAME = Global.getProperty("aliyun.sms.SignName");

    /**
     * @Method sendSms
     * @Description 发送短信
     * @Author AlanShelby
     * @Date 16:23 2019/8/26 0026
     * @Param [phoneNumber, params]
     * @Return java.lang.String
     */
    public static String sendSms(String phoneNumber, String params) {
        DefaultProfile profile = DefaultProfile.getProfile("default", ACCESSKEY_ID, SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "default");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", SIGN_NAME);
        request.putQueryParameter("TemplateCode", TEMPLATE_CODE);
        request.putQueryParameter("TemplateParam", params);

        CommonResponse response = null;
        try {
            response = client.getCommonResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return response.getData();
    }

    /**
     * @Method generateValidationCode
     * @Description 生成6位随机验证码
     * @Author AlanShelby
     * @Date 15:48 2019/8/26 0026
     * @Return java.lang.String
     */
    public static String generateValidationCode() {
        String random = (int) ((Math.random() * 9 + 1) * 100000) + "";
        return random;
    }
}
